package com.liukai.novel_rack.presenter.impl;

import android.util.Log;

import com.liukai.novel_base.base.BasePresenter;
import com.liukai.novel_base.base.IBaseCallBack;
import com.liukai.novel_commons.API;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.entities.UserInfo;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.RetrofitManager;
import com.liukai.novel_rack.call_back.IRackCallBack;
import com.liukai.novel_rack.presenter.IRackPresenter;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class RackPresenterImpl extends BasePresenter<IRackCallBack> implements IRackPresenter {

    @Override
    public void findAllRackByUserId(String userId) {

        Retrofit retrofit = RetrofitManager.getRackInstance().getRetrofit();
            API api = retrofit.create(API.class);
            Call<Result> findRackTask = api.findAllRackByUserId(userId);
        findRackTask.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(),"response:" + response.toString());
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK){
                    LogUtils.e(getClass(),"body:" + response.body());
                    Result result = response.body();
                    mCallBack.findAllRackByUserIdResult(result);
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });

    }

    @Override
    public void loadMuLu(String novelId) {
        Retrofit retrofit = RetrofitManager.getNovelInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> loadMuLu = api.loadMuLu(novelId);
        loadMuLu.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(),"response:" + response.toString());
                LogUtils.e(getClass(),"body:" + response.body().toString());
                if (response.code() == HttpURLConnection.HTTP_OK){
                    Result body = response.body();
                    mCallBack.loadMuLuResult(body);
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                LogUtils.e(getClass(),"Throwable:" + t.getMessage());

            }
        });
    }

    @Override
    public void deleteRackById(NovelRack novelRack) {
        Retrofit retrofit = RetrofitManager.getRackInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> deleteRackById = api.deleteRackById(novelRack.getNovelId(),novelRack.getUserId());
        deleteRackById.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(),response.toString());
                LogUtils.e(getClass(),response.body().toString());
                mCallBack.deleteRackById(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

}
